
#include <windows.h>
#include "respool.h"

char*   respool_alloc (respool * pool, int size)
{
  char * r;
  int new_size;

  size +=  ( sizeof(void*) - 1);
  size &= ~( sizeof(void*) - 1);

  if (pool->buff_size >= size)
  {
    r = pool->buff;
    pool->buff += size;
    pool->buff_size -= size;

    return r;
  }

  new_size = size > 4000 ? size + sizeof (void*) : 4096;

  r = (char*) malloc (new_size );

  if (r == NULL)
  {
    return r;
  };

  *((char**)r) = pool->pool;
  pool->pool = r;

  r += sizeof(void*);

  if (size < 4000)
  {
    pool->buff      = r + size;
    pool->buff_size = 4096 - sizeof(void*) - size;
  };

  pool->total_size += new_size;
  return r;
};

void    respool_close (respool * pool)
{
  while ( pool->pool )
  {
    char* next = *( (char**)(pool->pool) );
    free (pool->pool);
    pool->pool = next;
  };
}


